package com.ruoyi.system.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Courses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.FitnesscheckinsMapper;
import com.ruoyi.system.domain.Fitnesscheckins;
import com.ruoyi.system.service.IFitnesscheckinsService;

/**
 * 健身打卡信息Service业务层处理
 * 
 * @author 张英旺
 * @date 2024-02-27
 */
@Service
public class FitnesscheckinsServiceImpl implements IFitnesscheckinsService 
{
    @Autowired
    private FitnesscheckinsMapper fitnesscheckinsMapper;

    /**
     * 查询健身打卡信息
     * 
     * @param checkId 健身打卡信息主键
     * @return 健身打卡信息
     */
    @Override
    public Fitnesscheckins selectFitnesscheckinsByCheckId(Long checkId)
    {
        return fitnesscheckinsMapper.selectFitnesscheckinsByCheckId(checkId);
    }

    /**
     * 查询健身打卡信息列表
     * 
     * @param fitnesscheckins 健身打卡信息
     * @return 健身打卡信息
     */
    @Override
    public List<Fitnesscheckins> selectFitnesscheckinsList(Fitnesscheckins fitnesscheckins)
    {
        return fitnesscheckinsMapper.selectFitnesscheckinsList(fitnesscheckins);
    }

    /**
     * 新增健身打卡信息
     * 
     * @param fitnesscheckins 健身打卡信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFitnesscheckins(Fitnesscheckins fitnesscheckins)
    {
        fitnesscheckins.setCreateTime(DateUtils.getNowDate());
        int rows = fitnesscheckinsMapper.insertFitnesscheckins(fitnesscheckins);
        insertSysUser(fitnesscheckins);
        return rows;
    }

    /**
     * 修改健身打卡信息
     * 
     * @param fitnesscheckins 健身打卡信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFitnesscheckins(Fitnesscheckins fitnesscheckins)
    {
        fitnesscheckins.setUpdateTime(DateUtils.getNowDate());
        fitnesscheckinsMapper.deleteSysUserByUserId(fitnesscheckins.getCheckId());
        insertSysUser(fitnesscheckins);
        return fitnesscheckinsMapper.updateFitnesscheckins(fitnesscheckins);
    }

    /**
     * 批量删除健身打卡信息
     * 
     * @param checkIds 需要删除的健身打卡信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFitnesscheckinsByCheckIds(Long[] checkIds)
    {
        fitnesscheckinsMapper.deleteSysUserByUserIds(checkIds);
        return fitnesscheckinsMapper.deleteFitnesscheckinsByCheckIds(checkIds);
    }

    /**
     * 删除健身打卡信息信息
     * 
     * @param checkId 健身打卡信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFitnesscheckinsByCheckId(Long checkId)
    {
        fitnesscheckinsMapper.deleteSysUserByUserId(checkId);
        return fitnesscheckinsMapper.deleteFitnesscheckinsByCheckId(checkId);
    }


//    @Override
//    public int insertFitnesscheckinsDate(Fitnesscheckins fitnesscheckins) {
//        //获取用户所要打卡的时间
//        Date checkinsDate = fitnesscheckins.getCheckDatetime();
//        //获取现在的时间
//        Date currentTime = new Date();
//        if(currentTime.equals(checkinsDate)){
//            fitnesscheckinsMapper.updateFitnesscheckins(fitnesscheckins);
//            return 1;
//        }
//        return 0;
//    }

    /**
     * 新增用户信息信息
     * 
     * @param fitnesscheckins 健身打卡信息对象
     */
    public void insertSysUser(Fitnesscheckins fitnesscheckins)
    {
        List<SysUser> sysUserList = fitnesscheckins.getSysUserList();
        Long checkId = fitnesscheckins.getCheckId();
        if (StringUtils.isNotNull(sysUserList))
        {
            List<SysUser> list = new ArrayList<SysUser>();
            for (SysUser sysUser : sysUserList)
            {
                sysUser.setUserId(checkId);
                list.add(sysUser);
            }
            if (list.size() > 0)
            {
                fitnesscheckinsMapper.batchSysUser(list);
            }
        }
    }
    /**
     * 新增健身课程信息信息
     *
     * @param fitnesscheckins 健身打卡信息对象
     */
    public void insertCourses(Fitnesscheckins fitnesscheckins)
    {
        List<Courses> coursesList = fitnesscheckins.getCoursesList();
        Long checkId = fitnesscheckins.getCheckId();
        if (StringUtils.isNotNull(coursesList))
        {
            List<Courses> list = new ArrayList<Courses>();
            for (Courses courses : coursesList)
            {
                courses.setCourseId(checkId);
                list.add(courses);
            }
            if (list.size() > 0)
            {
                fitnesscheckinsMapper.batchCourses(list);
            }
        }
    }
}
